//
//  LoginViewController.m
//  tingche
//
//  Created by 尤振宇 on 2017/6/21.
//  Copyright © 2017年 尤振宇. All rights reserved.
//

#import "LoginViewController.h"
#import "WKCountdownButton.h"
#import "LoginApi.h"
#import "Config.h"
#import "VerificationCodeApi.h"
#import <EaseUI.h>

@interface LoginViewController ()
@property (weak, nonatomic) IBOutlet UIButton *passworkBtn;
@property (weak, nonatomic) IBOutlet UIButton *messageBtn;

@property (weak, nonatomic) IBOutlet UITextField *accountTF;
@property (weak, nonatomic) IBOutlet UITextField *passworkTF;

@property (weak, nonatomic) IBOutlet WKCountdownButton *getDynamicBtn;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *getDynamicW;
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;

@end

@implementation LoginViewController

+(instancetype)LoginViewController:(UIViewController *)viewController
{
    LoginViewController *vc = [[LoginViewController alloc]init];
    [viewController presentViewController:vc animated:YES completion:nil];
    return vc;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    NSString *mobile = [Config userDefaultsGetObjectForKey:kMobile];
    if (mobile) {
        self.accountTF.text = mobile;
    }
    [self.getDynamicBtn roundedCorners_5PX];
    [self.loginBtn roundedCorners_5PX];
//    [self switchAction:self.passworkBtn];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)dismissAction:(id)sender {
    
    [self.getDynamicBtn stop];
    [self.accountTF resignFirstResponder];
    [self.passworkTF resignFirstResponder];
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)switchAction:(id)sender {
    UIButton *select = sender == self.passworkBtn ? self.passworkBtn : self.messageBtn;
    UIButton *noSelect = sender == self.passworkBtn ? self.messageBtn : self.passworkBtn;
    
    select.selected = YES;
    noSelect.selected = NO;
    
    [self.view layoutIfNeeded];
    [UIView animateWithDuration:0.3 animations:^{
         self.passworkTF.text = @"";
         if (select == self.passworkBtn) {
             self.passworkTF.placeholder = @"请输入密码";
             self.passworkTF.secureTextEntry = YES;
             self.getDynamicW.constant = 0;
             self.getDynamicBtn.alpha = 0;
         }else{
             self.passworkTF.placeholder = @"请输入动态密码";
             self.passworkTF.secureTextEntry = NO;
             self.getDynamicW.constant = 84;
             self.getDynamicBtn.alpha = 1;
         }
         [self.view layoutIfNeeded]; // Called on parent view
        
    }];
    
}

- (IBAction)getDyamicAction:(id)sender {
    [self.accountTF resignFirstResponder];
    if (![self.accountTF.text validateMobile]) {
        [MBProgressHUD showError:@"请输入正确手机号"];
        return;
    }
    
    [self.getDynamicBtn start];
    
    VerificationCodeApi *api = [[VerificationCodeApi alloc]initWithMoblle:self.accountTF.text];
    [api startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
        
        if (kCode(request.responseJSONObject)) {
            [self.accountTF resignFirstResponder];
            [MBProgressHUD showSuccess:[request.responseJSONObject objectForKey:@"message"]];
        }else{
            [self.getDynamicBtn stop];
        }
        
    } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
        [self.getDynamicBtn stop];
    }];
}

- (IBAction)loginAction:(id)sender {
    
    if (![self.accountTF.text validateMobile]) {
        [MBProgressHUD showError:@"请输入正确手机号"];
        return;
    }
    if (![self.passworkTF hasText]) {
        if (self.passworkBtn.selected) {
            [MBProgressHUD showError:@"请输入密码"];
        }else{
            [MBProgressHUD showError:@"请输入验证码"];
        }
        return;
    }
    
    LoginApi *api = [[LoginApi alloc]initWithMoblle:self.accountTF.text parameter:self.passworkTF.text];
    [api startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
        
        if (kCode(request.responseJSONObject)) {
            
            [Config SetToken:[request.responseHeaders objectForKey:@"Authorization"]];
            
            NSDictionary *dic = [request.responseJSONObject objectForKey:@"data"];
            
            [[ConsultantInformation sharedInformation] setValuesForKeysWithDictionary:dic];
            [[NSNotificationCenter defaultCenter]postNotificationName:kLoginNotification object:nil];
            
            [Config userDefaultsSetObject:[ConsultantInformation sharedInformation].mobile key:kMobile];
            
            NSString *hxUser = [ConsultantInformation sharedInformation].hx_username;
            NSString *hxPassword = [ConsultantInformation sharedInformation].hx_password;
            
            ///注册环信
            [[EMClient sharedClient]loginWithUsername:hxUser password:hxPassword completion:^(NSString *aUsername, EMError *aError) {
                if (!aError) {
                    [[EMClient sharedClient] updatePushNotifiationDisplayName:[ConsultantInformation sharedInformation].name completion:^(NSString *aDisplayName, EMError *aError) {
                    }];
                    [[EMClient sharedClient].options setIsAutoLogin:YES];
                }
            }];
            
            [self dismissAction:nil];
        }
    } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
        
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
